#!/bin/bash

# 更新软件源
#sudo yum update -y
pkg=$1
# 安装软件包（示例中使用 "vim" 作为要安装的软件包）
sudo yum install -y "${pkg:-"tree"}" > /dev/null 2>&1 &

# 定义进度条函数
function show_progress() {
  local pid=$1
  local delay=0.2
  local width=40

  while [ -e /proc/$pid ]; do
    local progress=$(ps -p $pid -o %cpu --no-headers)
    local scaled_progress=$((progress / 10))
    local completed=$scaled_progress
    local remaining=$((width - completed))

    printf "["
    for ((i=0; i<completed; i++)); do
      printf "="
    done
    for ((i=0; i<remaining; i++)); do
      printf " "
    done
    printf "] %d%%" "$progress"
    sleep $delay
    printf "\r"
  done
  printf "\n"
}

# 获取安装命令的进程 ID
install_pid=$!

# 显示进度条
show_progress $install_pid

echo "软件 ${pkg:-"tree"} 安装完成"